স্প্রিং বুট ক্লায়েন্টে API Gateway এর মাধ্যমে Rate Limiting এবং Authentication কনফিগারেশন করার জন্য সাধারণত Spring Cloud Gateway এবং Spring Security ব্যবহার করা হয়। এটি মাইক্রোসার্ভিস আর্কিটেকচারে API গেটওয়ের মাধ্যমে রিকোয়েস্ট পরিচালনা এবং সুরক্ষা নিশ্চিত করতে সহায়তা করে।
Spring Cloud Gateway একটি API গেটওয়ে হিসেবে কাজ করে এবং এটি Rate Limiting এবং Authentication পরিচালনা করতে সহায়ক। নিচে এর কনফিগারেশন দেয়া হলো।
Maven:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Gradle:
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
implementation 'org.springframework.boot:spring-boot-starter-security'
Spring Cloud Gateway এ Rate Limiting কনফিগার করতে Redis ব্যবহার করা হয়। এর মাধ্যমে নির্দিষ্ট সময়ের মধ্যে কতগুলো রিকোয়েস্ট অনুমোদিত হবে তা কনফিগার করা যায়।
application.yml
ফাইলে Rate Limiting কনফিগারেশন:spring:
cloud:
gateway:
routes:
- id: api_route
uri: http://example-service
predicates:
- Path=/api/**
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20
redis-rate-limiter.requestedTokens: 1
redis:
host: localhost
port: 6379
replenishRate
: প্রতি সেকেন্ডে কতগুলো টোকেন রিফিল হবে।burstCapacity
: একসাথে সর্বোচ্চ কতগুলো রিকোয়েস্ট অনুমোদিত।requestedTokens
: প্রতি রিকোয়েস্টের জন্য কতগুলো টোকেন প্রয়োজন।Spring Cloud Gateway এ Authentication সেটআপ করার জন্য Spring Security ব্যবহার করা হয়। সাধারণত JWT (JSON Web Token) এর মাধ্যমে Authentication এবং Authorization কনফিগার করা হয়।
SecurityConfig.java
এ Spring Security কনফিগারেশন:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**").authenticated() // Only authenticated users can access /api/**
.anyRequest().permitAll()
.and()
.oauth2Login(); // Using OAuth2 login
}
}
JWT ব্যবহার করে API Gateway এর মাধ্যমে Authentication কনফিগার করার উদাহরণ:
Spring Security তে JWT Authentication ফিল্টার যোগ করতে হবে যা গেটওয়ের জন্য সমস্ত রিকোয়েস্টকে যাচাই করবে।
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component
public class JwtAuthenticationFilter extends OncePerRequestFilter {
private static final String SECRET_KEY = "your_secret_key"; // Secret key for signing JWT
@Override
protected void doFilterInternal(HttpServletRequest request, javax.servlet.http.HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String token = request.getHeader("Authorization");
if (token != null && token.startsWith("Bearer ")) {
token = token.substring(7); // Remove "Bearer " prefix
try {
String username = Jwts.parser()
.setSigningKey(SECRET_KEY)
.parseClaimsJws(token)
.getBody()
.getSubject();
if (username != null) {
// Authentication logic here (populate SecurityContext)
}
} catch (Exception e) {
response.setStatus(403); // Forbidden
}
}
filterChain.doFilter(request, response); // Continue the filter chain
}
}
JWT Authentication Filter যোগ করতে WebSecurityConfigurerAdapter ক্লাসে আপডেট করুন:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final JwtAuthenticationFilter jwtAuthenticationFilter;
public SecurityConfig(JwtAuthenticationFilter jwtAuthenticationFilter) {
this.jwtAuthenticationFilter = jwtAuthenticationFilter;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**").authenticated() // Only authenticated users can access /api/**
.anyRequest().permitAll()
.and()
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); // Add JWT filter
}
}
Spring Cloud Gateway এ API Rate Limiting এবং Authentication একত্রে কনফিগার করতে application.yml ফাইলে দুইটি ফিচারই কনফিগার করা যায়।
spring:
cloud:
gateway:
routes:
- id: api_route
uri: http://example-service
predicates:
- Path=/api/**
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20
redis-rate-limiter.requestedTokens: 1
- name: AddRequestHeader
args:
name: Authorization
value: "Bearer your_token_here"
redis:
host: localhost
port: 6379
@EnableWebSecurity
ব্যবহার করে Spring Security কনফিগারেশন সম্পন্ন করতে পারবেন।এই পদ্ধতিগুলো ব্যবহার করে আপনি Spring Cloud Gateway এর মাধ্যমে Rate Limiting এবং Authentication কনফিগার করতে পারবেন।
Read more